package com.ojoin.trade.t0loan_core.web.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ojoin.trade.common.web.service.impl.HotConfigService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.base.Splitter;
import com.google.common.collect.Sets;
import com.ojoin.trade.common.autoconfig.api.IChangeListener;
import com.ojoin.trade.common.autoconfig.api.IConfig;
import com.ojoin.trade.common.constdef.IConstDef;
import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.Config;
import com.ojoin.trade.common.utils.DownloadUtils;
import com.ojoin.trade.common.utils.EmailSendUtil;
import com.ojoin.trade.common.utils.FileUploadUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.common.utils.paynow.IAccountConst;
import com.ojoin.trade.common.web.controller.SimpleBaseController;
import com.ojoin.trade.common.web.domain.Result;
import com.ojoin.trade.common.web.domain.TbWeekend;
import com.ojoin.trade.common.web.domain.Result.Status;
import com.ojoin.trade.common.web.service.SysManageService;
import com.ojoin.trade.common.web.service.TbWeekEndService;
import com.ojoin.trade.common_core.utils.TradeUtils;
import com.ojoin.trade.common_core.web.domain.TbMerchantInfo;
import com.ojoin.trade.common_core.web.domain.TbMerchantUser;
import com.ojoin.trade.common_core.web.domain.TbMerchantuserFinaccount;
import com.ojoin.trade.common_core.web.domain.TbTransferTokenStatus;
import com.ojoin.trade.common_core.web.domain.TransferErrorStatusInfo;
import com.ojoin.trade.common_core.web.service.AccountBaseService;
import com.ojoin.trade.common_core.web.service.FactoringCompanyBaseService;
import com.ojoin.trade.common_core.web.service.FactoringOperatorBaseService;
import com.ojoin.trade.common_core.web.service.MerchantUserBaseService;
import com.ojoin.trade.common_core.web.service.TbMerchantInfoBaseService;
import com.ojoin.trade.common_core.web.service.TbMerchantuserFinaccountService;
import com.ojoin.trade.common_core.web.service.TransferTokenStatusService;
import com.ojoin.trade.common_core.web.service.impl.DataVersionValidationService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlLoanService;
import com.ojoin.trade.t0loan_core.web.domain.T0LoanCheckAccountInfoVO;
import com.ojoin.trade.t0loan_core.web.domain.T0LoanLiveUserHistoryTransTotalInfoVO;
import com.ojoin.trade.t0loan_core.web.domain.T0LoanTransTotalInfoVO;
import com.ojoin.trade.t0loan_core.web.domain.T0LoanTransanctionLogInfoVO;
import com.ojoin.trade.t0loan_core.web.domain.TxLoanTransTotalInfoVO;
import com.ojoin.trade.t0loan_core.web.domain.TbOutofSinglePosTranscationLog;
import com.ojoin.trade.t0loan_core.web.domain.TbPosTranscationLog;
import com.ojoin.trade.t0loan_core.web.domain.TbT0LoanLiveApplication;
import com.ojoin.trade.t0loan_core.web.domain.TbT0LoanLiveInfoVO;
import com.ojoin.trade.t0loan_core.web.domain.TbT0LoanTodoTaskVO;
import com.ojoin.trade.t0loan_core.web.domain.TbT0LoanTransanctionLog;
import com.ojoin.trade.t0loan_core.web.service.FtpFileBaseService;
import com.ojoin.trade.t0loan_core.web.service.PosTranscationLogBaseService;
import com.ojoin.trade.t0loan_core.web.service.T0LoanBaseService;
import com.ojoin.trade.t0loan_core.web.service.T0LoanFinalBaseService;
import com.ojoin.trade.t0loan_core.web.service.T0LoanLiveBaseService;
import com.ojoin.trade.t0loan_core.web.service.T0LoanTransanctionLogBaseService;

@Controller
@RequestMapping("/operate")
public class OperatorUserController extends SimpleBaseController  {

	private Logger log = LoggerFactory.getLogger(OperatorUserController.class);

    @Autowired
    private HotConfigService hotConfigService;


	@Autowired
	private SysManageService systemService;

	@Autowired
	private DataVersionValidationService validateService;

	@Autowired
	private AccountBaseService accountbaseservice;

	@Autowired
	private TbWeekEndService tbWeekEndService;

	public DataVersionValidationService getValidateService() {
		return validateService;
	}

	@Autowired
	private PosTranscationLogBaseService posservice;

	public PosTranscationLogBaseService getPosservice() {
		return posservice;
	}

	@Autowired
	private T0LoanBaseService loanservice;

	public T0LoanBaseService getLoanservice() {
		return loanservice;
	}

	@Autowired
	private TbMerchantInfoBaseService infoservice;

	public TbMerchantInfoBaseService getInfoservice() {
		return infoservice;
	}

	@Autowired
	private AccountBaseService accountservice;

	public AccountBaseService getAccountservice() {
		return accountservice;
	}

	@Autowired
	private T0LoanLiveBaseService loanliveservice;

	public T0LoanLiveBaseService getLoanliveservice() {
		return loanliveservice;
	}

	@Autowired
	private T0LoanFinalBaseService loanfinalservice;

	public T0LoanFinalBaseService getLoanfinalservice() {
		return loanfinalservice;
	}

	@Autowired
	private T0LoanTransanctionLogBaseService translogservice;

	public T0LoanTransanctionLogBaseService getTranslogservice() {
		return translogservice;
	}

	@Autowired
	private TransferTokenStatusService transferstatusservice;

	public TransferTokenStatusService getTransferstatusservice() {
		return transferstatusservice;
	}

	@Autowired
	private FactoringOperatorBaseService operateservice;

	public FactoringOperatorBaseService getOperateservice() {
		return operateservice;
	}

	@Autowired
	private FactoringCompanyBaseService companyservice;

	public FactoringCompanyBaseService getCompanyservice() {
		return companyservice;
	}

	@Autowired
	private FtpFileBaseService ftpservice;

	public FtpFileBaseService getFtpservice() {
		return ftpservice;
	}

	@Autowired
	private TbSlLoanService tbSlLoanService;

	@ResponseBody
	@RequestMapping(value = "/queryLiveInfoByStatus", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryLiveInfoByStatus(@RequestParam String loanstatus,
			Pageable page) {
		if (StringUtils.isEmpty(loanstatus)) {
			return new Result(Status.ERROR, "待查询审核状态为空，请检查.");
		}
		try {
			List<TbT0LoanLiveInfoVO> livelist = getLoanliveservice()
					.queryAllT0LoanLiveInfoByStatus(
							CommonUtils.getInt(loanstatus), page);
			return new Result(Status.OK, livelist);
		} catch (Exception e) {
			log.error("查询状态为" + loanstatus + "的T+0贷款申请记录报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());

			return new Result(Status.ERROR, "查询状态为" + loanstatus
					+ "的T+0贷款申请记录报错，请重试");
		}
	}



	// 查询申请T+0贷款审核通过的九盈用户的当日前30日历史交易金额合计列表
	@ResponseBody
	@RequestMapping(value = "/queryT0LoanLiveUserHistoryTransTotalInfoList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryT0LoanLiveUserHistoryTransTotalInfoList(
			HttpServletRequest request, Pageable page) {
		try {
			List<T0LoanLiveUserHistoryTransTotalInfoVO> list = getLoanliveservice()
					.queryT0LoanLiveUserHistoryTransTotalInfoList(page);
			return new Result(Status.OK, list);
		} catch (Exception e) {
			log.error("查询申请T+0贷款审核通过的九盈用户的当日前30日历史交易金额合计列表报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());

			return new Result(Status.ERROR,
					"查询申请T+0贷款审核通过的九盈用户的当日前30日历史交易金额合计列表报错，请重试");
		}
	}

	// 查询指定商户申请T+0贷款审核通过的九盈用户的当日前30日历史交易列表
	@ResponseBody
	@RequestMapping(value = "/queryT0LoanLiveUserHistoryPosTransListByMhtNo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryT0LoanLiveUserHistoryPosTransListByMhtNo(
			@RequestBody String mhtno, Pageable page) {
		if (StringUtils.isEmpty(mhtno)) {
			return new Result(Status.ERROR, "商户号为空，请检查.");
		}
		try {
			List<TbPosTranscationLog> loglist = getPosservice()
					.queryT0LoanLiveUserHistoryPosTransListByMhtNo(mhtno, page);
			return new Result(Status.OK, loglist);
		} catch (Exception e) {
			log.error("查询指定商户申请T+0贷款审核通过的九盈用户的当日前30日历史交易列表报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());

			return new Result(Status.ERROR,
					"查询指定商户申请T+0贷款审核通过的九盈用户的当日前30日历史交易列表报错，请重试");
		}
	}

	// //授信
	// @ResponseBody
	// @RequestMapping(value = "/creditSet", method = RequestMethod.POST,
	// produces = MediaType.APPLICATION_JSON_VALUE)
	// public Result creditSet(HttpServletRequest request,
	// @RequestParam String liveid,
	// @RequestParam String joinuserid,
	// @RequestParam String fee,
	// @RequestParam String ratio,
	// @RequestParam String expected_loan_time,
	// @RequestParam String rm_daily_threshold,
	// @RequestParam String rm_single_threshold) {
	// if(StringUtils.isEmpty(liveid)){
	// return new Result(Status.ERROR, "申请记录为空，请检查.");
	// }
	// if(StringUtils.isEmpty(joinuserid)){
	// return new Result(Status.ERROR, "九盈用户id为空，请检查.");
	// }
	// // if(StringUtils.isEmpty(fee)){
	// // return new Result(Status.ERROR, "手续费为空，请检查.");
	// // }
	// if(StringUtils.isEmpty(ratio)){
	// return new Result(Status.ERROR, "利率为空，请检查.");
	// }
	// if(StringUtils.isEmpty(expected_loan_time)){
	// return new Result(Status.ERROR, "期望放款时间为空，请检查.");
	// }
	// if(StringUtils.isEmpty(rm_daily_threshold)){
	// return new Result(Status.ERROR, "日交易限额为空，请检查.");
	// }
	// if(StringUtils.isEmpty(rm_single_threshold)){
	// return new Result(Status.ERROR, "单笔交易限额为空，请检查.");
	// }
	// try {
	// getLoanfinalservice().creditSet(Toolkit.getInt(liveid),
	// Toolkit.getInt(joinuserid),
	// fee, ratio, expected_loan_time, rm_daily_threshold, rm_single_threshold);
	// return new Result(Status.OK, "授信成功");
	// } catch (Exception e) {
	// log.error("授信操作报错", e);
	// return new Result(Status.ERROR, "授信操作报错，请重试");
	// }
	// }


	
	//正常商户列表 -- 查询所有已申请成功T+0贷款的可放贷九盈用户交易金额合计列表记录
	@ResponseBody
	@RequestMapping(value = "/queryAllT0LoanJoinUserTransanctionTotalList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryAllT0LoanJoinUserTransanctionTotalList(HttpServletRequest request, Pageable page) {
		try {
			List<T0LoanTransTotalInfoVO> list = getLoanfinalservice().queryAllT0LoanJoinUserTransanctionTotalList(page);
			return new Result(Status.OK, list);
		} catch (Exception e) {
			log.error("查询所有已申请成功T+0贷款的可放贷九盈用户交易金额合计列表记录报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());
			
			return new Result(Status.ERROR, "查询所有已申请成功T+0贷款的可放贷九盈用户交易金额合计列表记录报错，请重试");
		}
	}	
	

	// 查询当日指定支付商户未申请T+1贷款的Pos机交易明细记录
	@ResponseBody
	@RequestMapping(value = "/queryAllNoT1LoanPosTransInfoListByPayUserID", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryAllNoT1LoanPosTransInfoListByPayUserID(
			HttpServletRequest request, @RequestParam int joinuserid,
			@RequestParam String transdate, @RequestParam String transtime,
			Pageable page) {
		if (joinuserid == 0) {
			return new Result(Status.ERROR, "商户id为空，请检查.");
		}
		if (StringUtils.isEmpty(transdate)) {
			return new Result(Status.ERROR, "交易查询截止日期为空，请检查.");
		}
		if (StringUtils.isEmpty(transtime)) {
			return new Result(Status.ERROR, "交易查询截止时间为空，请检查.");
		}

		try {
			List<TbPosTranscationLog> list = getPosservice()
					.queryAllNoT1LoanPosTransInfoListByPayUserID(joinuserid,
							transdate);
			return new Result(Status.OK, list);
		} catch (Exception e) {
			log.error("查询当日指定支付商户未申请T+0贷款的Pos机交易明细记录报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());

			return new Result(Status.ERROR,
					"查询当日指定支付商户未申请T+0贷款的Pos机交易明细记录报错，请重试");
		}
	}

	// 日额度超限商户列表
	@ResponseBody
	@RequestMapping(value = "/queryAllOutOfDayJoinUserTransanctionTotalList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryAllOutOfDayJoinUserTransanctionTotalList(
			HttpServletRequest request, Pageable page) {
		try {
			List<T0LoanTransTotalInfoVO> list = getLoanfinalservice()
					.queryAllOutOfDayJoinUserTransanctionTotalList(page);
			return new Result(Status.OK, list);
		} catch (Exception e) {
			log.error("查询日额度超限商户列表报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());

			return new Result(Status.ERROR, "查询日额度超限商户列表报错，请重试");
		}
	}

	// 单笔额度超限商户列表
	@ResponseBody
	@RequestMapping(value = "/queryAllOutofSingleJoinUserTransanctionTotalList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryAllOutofSingleJoinUserTransanctionTotalList(
			HttpServletRequest request, Pageable page) {
		try {
			List<T0LoanTransTotalInfoVO> list = getLoanfinalservice()
					.queryAllOutofSingleJoinUserTransanctionTotalList(page);
			return new Result(Status.OK, list);
		} catch (Exception e) {
			log.error("查询单笔额度超限商户列表报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());

			return new Result(Status.ERROR, "查询单笔额度超限商户列表报错，请重试");
		}
	}

	// 商户单笔额度超限交易记录列表
	@ResponseBody
	@RequestMapping(value = "/queryOutofSinglePosTranscationLogListByMhtno", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryOutofSinglePosTranscationLogListByMhtno(
			HttpServletRequest request, String mhtno, Pageable page) {
		try {
			List<TbOutofSinglePosTranscationLog> list = getLoanfinalservice()
					.queryOutofSinglePosTranscationLogListByMhtno(mhtno, page);
			return new Result(Status.OK, list);
		} catch (Exception e) {
			log.error("查询商户单笔额度超限交易记录列表报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());

			return new Result(Status.ERROR, "查询商户单笔额度超限交易记录列表报错，请重试");
		}
	}



	// 查询T0贷款对账信息列表
	@ResponseBody
	@RequestMapping(value = "/queryAllT0LoanCheckAccountInfoList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryT0LoanCheckAccountInfoListByDate(
			HttpServletRequest request, Pageable page) {
		try {
			List<T0LoanCheckAccountInfoVO> list = getTranslogservice()
					.queryAllT0LoanCheckAccountInfoList(page);
			return new Result(Status.OK, list);
		} catch (Exception e) {
			log.error("查询T0贷款对账信息列表报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());

			return new Result(Status.ERROR, "查询T0贷款对账信息列表报错，请重试");
		}
	}

	@ResponseBody
	@RequestMapping(value = "/queryAccBaseTransDetail", method = RequestMethod.GET)
	public Result queryAccountBaseTransactionDetail(HttpServletRequest request,
			String token, Integer id) {
		try {
			TbTransferTokenStatus info = transferstatusservice.queryById(id);
			List list = accountbaseservice
					.queryAccountBaseTransactionDetail(token);
			if ("00".equals(list.get(0))) {
				Map<String, String> data = (Map) list.get(1);
				if ("02".equals(data.get("responseCode"))) {

					String tradeData = "{'memo':" + info.getMemo()
							+ ";'mhtNoFrom':" + info.getFromIdOfPaynow() + ";"
							+ "'mhtNoTo':" + info.getToIdOfPaynow()
							+ ";'orderAmt':" + info.getAmount()
							+ ";'tradeStatus':02;'transDate':"
							+ info.getRowUpdateTime() + "}";
					return new Result(Status.OK, tradeData);
				} else {
					String tradeData = data.get("tradeData");
					if (StringUtils.isEmpty(tradeData)) {
						return new Result(Status.OK, null);
					}
					return new Result(Status.OK, tradeData);
				}

			} else {
				String tradeData = "{'memo':" + info.getMemo()
						+ ";'mhtNoFrom':" + info.getFromIdOfPaynow() + ";+"
						+ "'mhtNoTo':" + info.getToIdOfPaynow()
						+ ";'orderAmt':" + info.getAmount()
						+ ";'tradeStatus':01;'transDate':"
						+ info.getRowUpdateTime() + "}";
				return new Result(Status.OK, tradeData);

			}
		} catch (Exception e) {
			log.error("查询失败", e);
			return new Result(Status.ERROR, "查询失败");
		}
	}

	// 查询转账状态异常列表
	@ResponseBody
	@RequestMapping(value = "/queryErrorTransferList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryErrorTransferList(HttpServletRequest request,
			Pageable page) {
		try {
			List<TransferErrorStatusInfo> list = getTransferstatusservice()
					.queryDoingTransferList(page);
			return new Result(Status.OK, list);
		} catch (Exception e) {
			log.error("查询转账状态异常列表报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());

			return new Result(Status.ERROR, "查询转账状态异常列表报错，请重试");
		}
	}

	/*
	 * 修改T+0贷款申请记录的授信额度、生成相关文件，并更新状态为3 (T0000035) 说明： 1)、调用金融账户开户(JA000012)
	 * 2)、生成保理合同pdf文件，填充相关内容 3)、生成账户变更申请表文件，填充相关内容
	 * 4)、更新授信额度及保理合同、账户变更申请表文件路径及状态为3
	 */

	@ResponseBody
	@RequestMapping(value = "/updateT0LoanLiveInfoLimitByLiveID", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result updateT0LoanLiveInfoLimitByLiveID(HttpServletRequest request,
			int liveid, int joinuserid, String rm_daily_threshold,
			String rm_single_threshold, String qryprintscreen, String pagets) {

		if (liveid == 0) {
			return new Result(Status.ERROR, "贷款申请记录id为空，请检查.");
		}
		if (joinuserid == 0) {
			return new Result(Status.ERROR, "贷款申请用户id为空，请检查.");
		}
		if (StringUtils.isEmpty(rm_daily_threshold)) {
			return new Result(Status.ERROR, "日限额为空，请检查.");
		}
		if (StringUtils.isEmpty(rm_single_threshold)) {
			return new Result(Status.ERROR, "单笔限额为空，请检查.");
		}
		if (StringUtils.isEmpty(qryprintscreen)) {
			return new Result(Status.ERROR, "授信查询凭证截图为空，请检查.");
		}

		// 操作数据版本校验
		TbT0LoanLiveApplication validateVO = new TbT0LoanLiveApplication();
		validateVO.setId(liveid);
		validateService.validateVO(validateVO, pagets);

		String filePath = FileUploadUtils.getDefaultBaseDir() + File.separator
				+ IConstDef.contractGenFileDir;

		try {
			// 日限额和单笔限额前端填写单位为元，存储前需要乘以100变更为单位为分
			rm_daily_threshold = CommonUtils.getString(CommonUtils
					.getLong(rm_daily_threshold) * 100);
			rm_single_threshold = CommonUtils.getString(CommonUtils
					.getLong(rm_single_threshold) * 100);

			getLoanliveservice().updateT0LoanLiveInfoLimitByLiveID(liveid,
					joinuserid, rm_daily_threshold, rm_single_threshold,
					qryprintscreen, filePath);
			return new Result(Status.OK, liveid);
		} catch (Exception e) {
			log.error("修改T+0贷款申请记录的授信额度、生成相关文件，并更新状态为3报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());

			return new Result(Status.ERROR,
					"修改T+0贷款申请记录的授信额度、生成相关文件，并更新状态为3报错，请重试");
		}
	}

	/*
	 * 更新T+0贷款申请记录的状态为5(账户变更申请表审核通过)
	 */
	@ResponseBody
	@RequestMapping(value = "/updateT0LoanLiveInfoUserCommitPass", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result updateT0LoanLiveInfoUserCommitPass(
			HttpServletRequest request, int liveid, String pagets) {
		if (liveid == 0) {
			return new Result(Status.ERROR, "贷款申请记录id为空，请检查.");
		}

		// 操作数据版本校验
		TbT0LoanLiveApplication validateVO = new TbT0LoanLiveApplication();
		validateVO.setId(liveid);
		validateService.validateVO(validateVO, pagets);

		try {
			getLoanliveservice().updateT0LoanLiveInfoUserCommitPass(liveid);
			return new Result(Status.OK, liveid);
		} catch (Exception e) {
			log.error("更新T+0贷款申请记录的状态为5(账户变更申请表审核通过)报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());

			return new Result(Status.ERROR,
					"更新T+0贷款申请记录的状态为5(账户变更申请表审核通过)报错，请重试");
		}
	}

	/*
	 * 更新T+0贷款申请记录的状态为7(账户变更成功，结算户存在余额)并新增贷款申请最终记录
	 */
	@ResponseBody
	@RequestMapping(value = "/updateT0LoanLiveInfoAccChangePassAndCreateFinalInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result updateT0LoanLiveInfoAccChangePassAndCreateFinalInfo(
			HttpServletRequest request, int liveid, String materials,
			String pagets) {
		if (liveid == 0) {
			return new Result(Status.ERROR, "贷款申请记录id为空，请检查.");
		}

		// 操作数据版本校验
		TbT0LoanLiveApplication validateVO = new TbT0LoanLiveApplication();
		validateVO.setId(liveid);
		validateService.validateVO(validateVO, pagets);

		if (StringUtils.isEmpty(materials)) {
			return new Result(Status.ERROR, "结算户余额查询截图文件为空，请检查.");
		}

		try {
			getLoanliveservice()
					.updateT0LoanLiveInfoAccChangePassAndCreateFinalInfo(
							liveid, materials);
			return new Result(Status.OK, liveid);
		} catch (Exception e) {
			log.error("更新T+0贷款申请记录的状态为7(账户变更成功，结算户存在余额)并新增贷款申请最终记录报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());

			return new Result(Status.ERROR,
					"更新T+0贷款申请记录的状态为7(账户变更成功，结算户存在余额)并新增贷款申请最终记录报错，请重试");
		}
	}



	@ResponseBody
	@RequestMapping(value = "/updateLiveInfoStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result updateLiveInfoStatus(HttpServletRequest request,
			@RequestParam String liveid, @RequestParam String loanstatus,
			@RequestParam String pagets) {
		if (StringUtils.isEmpty(liveid)) {
			return new Result(Status.ERROR, "贷款审核id为空，请检查.");
		}

		if (StringUtils.isEmpty(loanstatus)) {
			return new Result(Status.ERROR, "审核状态为空，请检查.");
		}

		// 操作数据版本校验
		TbT0LoanLiveApplication validateVO = new TbT0LoanLiveApplication();
		validateVO.setId(CommonUtils.getInt(liveid));
		validateService.validateVO(validateVO, pagets);

		try {
			getLoanliveservice().updateT0LoanLiveInfoByLiveIDsAndStatus(
					new int[] { CommonUtils.getInt(liveid) },
					CommonUtils.getInt(loanstatus));
			return new Result(Status.OK, "更新成功");
		} catch (Exception e) {
			log.error("查询状态为" + loanstatus + "的T+0贷款申请记录报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());

			return new Result(Status.ERROR, "查询状态为" + loanstatus
					+ "的T+0贷款申请记录报错，请重试");
		}
	}

	@ResponseBody
	@RequestMapping(value = "/updateT0LoanLiveInfoUserCommitNoPassByLiveID", method = RequestMethod.POST)
	public Result updateT0LoanLiveInfoUserCommitNoPassByLiveID(
			HttpServletRequest request, int liveid, String memo, String pagets) {
		if (liveid == 0) {
			return new Result(Status.ERROR, "贷款审核id为空，请检查.");
		}

		// 操作数据版本校验
		TbT0LoanLiveApplication validateVO = new TbT0LoanLiveApplication();
		validateVO.setId(liveid);
		validateService.validateVO(validateVO, pagets);

		try {
			memo = URLDecoder.decode(memo);
		} catch (Exception e) {

		}
		try {
			int count = getLoanliveservice()
					.updateT0LoanLiveInfoUserCommitNoPassByLiveID(liveid, memo);
			return new Result(Status.OK, count);
		} catch (Exception e) {
			log.error("审核用户提交的账户变更申请表不通过，提交不通过原因并更新状态为6报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());

			return new Result(Status.ERROR,
					"审核用户提交的账户变更申请表不通过，提交不通过原因并更新状态为6报错，请重试");
		}
	}

	@ResponseBody
	@RequestMapping(value = "/downloadT0LoanLiveUserCommitFile", method = RequestMethod.GET)
	public void downloadT0LoanLiveUserCommitFile(HttpServletRequest request,
			HttpServletResponse response, String filepath) {
		try {
			int lastIndexOf = filepath.lastIndexOf("/");
			String filename = filepath;
			if (lastIndexOf != -1)
				filename = filepath.substring(lastIndexOf + 1,
						filepath.length());

			String filePath = FileUploadUtils.getDefaultBaseDir()
					+ File.separator + filepath;
			DownloadUtils.download(request, response, filePath, filename);
		} catch (Exception e) {
			log.error("下载用户提交账户变更申请表报错", e);
		}
	}

	@ResponseBody
	@RequestMapping(value = "/queryJoinuserBalanceByMhtno", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Result queryJoinuserBalanceByMhtno(HttpServletRequest request,
			String mhtNo) {
		try {
			Pageable page = new PageRequest(IConstDef.PageAll,
					IConstDef.PageAll);
			List<T0LoanCheckAccountInfoVO> balanceInfoList = getTranslogservice()
					.queryT0LoanBalanceInfoListByDebtorMhtno(mhtNo, page);
			return new Result(Status.OK, balanceInfoList);
		} catch (Exception e) {
			log.error("查询指定商户mhtNo->" + mhtNo + "的实时T+0贷款负债信息列表报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());

			return new Result(Status.ERROR, "查询指定商户mhtNo->" + mhtNo
					+ "的实时T+0贷款负债信息列表报错，请重试");
		}
	}

	@ResponseBody
	@RequestMapping(value = "/queryOperatorTodoTaskList", method = RequestMethod.GET)
	public Result queryOperatorTodoTaskList(HttpServletRequest request) {
		try {
			List<TbT0LoanTodoTaskVO> list = getLoanliveservice()
					.queryOperatorTodoTaskList();
			return new Result(Status.OK, list);
		} catch (Exception e) {
			log.error("查询保理员代办任务列表报错", e);
			if (e instanceof BusinessException)
				return new Result(Status.ERROR, e.getMessage());

			return new Result(Status.ERROR, "查询保理员代办任务列表报错，请重试");
		}
	}

//	@ResponseBody
//	@RequestMapping(value = "/dealT0credit", method = RequestMethod.GET)
//	public Result dealcredit(HttpServletRequest request) {
//		try {
//			log.info("pos交易文件下载并处理线程启动, 时间->" + TimeUtils.getCurDateTime());
//			getFtpservice().saveDealT0FtpFile("loan.txt", "credit",
//					IConstDef.FTP_FILETYPE_T0_CREDIT);
//			log.info("pos交易文件下载并处理线程结束, 时间->" + TimeUtils.getCurDateTime());
//			return new Result(Status.OK, null);
//		} catch (Exception e) {
//			log.error("pos交易文件下载并处理报错，请查看日志后重试.", e);
//			if (e instanceof BusinessException)
//				return new Result(Status.ERROR, e.getMessage());
//
//			return new Result(Status.ERROR, "pos交易文件下载并处理报错，请查看日志后重试.");
//		}
//	}

	// @ResponseBody
	// @RequestMapping(value = "/dealreturn", method = RequestMethod.GET)
	// public Result dealreturn(HttpServletRequest request) {
	// try {
	// log.info("回款文件下载并处理线程启动, 时间->" + TimeUtils.getCurDateTime());
	// getFtpservice().dealFtpFile("debit.txt", "return",
	// IConstDef.FTP_FILETYPE_RETURN);
	// log.info("回款文件下载并处理线程结束, 时间->" + TimeUtils.getCurDateTime());
	// return new Result(Status.OK, null);
	// } catch (Exception e) {
	// log.error("回款文件下载并处理报错，请查看日志后重试.", e);
	// if (e instanceof BusinessException)
	// return new Result(Status.ERROR, e.getMessage());
	//
	// return new Result(Status.ERROR, "回款文件下载并处理报错，请查看日志后重试.");
	// }
	// }

	@ResponseBody
	@RequestMapping(value = "/synUpdateState", method = RequestMethod.GET)
	public Result synUpdateState(HttpServletRequest request, String token,
			String msg) {
		try {
			transferstatusservice.updateTransferTokenStatus(token,
					IConstDef.TRANSFERSTATUS_SUCCESS, msg);
			return new Result(Status.OK, "同步成功");
		} catch (Exception e) {
			if (e instanceof BusinessException) {
				return new Result(Status.ERROR, e.getMessage());
			}
			return new Result(Status.ERROR, "同步失败");
		}
	}

	@ResponseBody
	@RequestMapping(value = "/reTransferAccount", method = RequestMethod.GET)
	public Result reTransferAccount(HttpServletRequest request, Integer id,
			String msg) throws BusinessException {
		try {
			tbSlLoanService.updateRechargeAccount(id, msg);
			return new Result(Status.OK, "处理成功");

		} catch (Exception e) {
			if (e instanceof BusinessException) {
				return new Result(Status.ERROR, e.getMessage());
			} else {
				return new Result(Status.ERROR, "处理失败");
			}
		}
	}



}